<template>
  <div id="windowPackageManage4Spec" class="windowEditorTab">
    <el-form label-width="100px">
      <el-form-item label="打包地址">
        <el-input v-model="$store.state.obj_setting_map.rpm_package_server_link" placeholder="输入RPM目录打包地址"
                  disabled/>
      </el-form-item>
      <el-form-item label="打包后端地址">
        <el-input v-model="$store.state.obj_setting_map.rpm_package_server_link_host" disabled
                  placeholder="输入RPM目录打包地址，mock地址">
          <template #append>
            <el-input v-model="$store.state.obj_setting_map.rpm_package_server_link_username" disabled
                      placeholder="输入用户名">
              <template #append>
                <el-input v-model="$store.state.obj_setting_map.rpm_package_server_link_password" type="password"
                          disabled placeholder="输入密码"/>
              </template>
            </el-input>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="打包脚本">
        <el-input v-model="form_script.script_package_dir2rpm" autosize type="textarea" placeholder="打包脚本"/>
      </el-form-item>
    </el-form>
    <el-space wrap spacer="|">
      <el-button type="success" @click="package_dir4rpm" :loading="loading_package">打包</el-button>
    </el-space>
    <h3>在此页面中进行配置，将{{ dir_name }}打包成RPM，文件目录为：{{ dir_path }}
    </h3>
    <div>
      <h3>{{ package_result.msg }}</h3>
      <el-link :href="package_result.package_link" target="_blank" type="success">
        <h3>{{ package_result.package_name }}</h3>
      </el-link>
    </div>

    <el-input v-model="log_package" autosize type="textarea" disabled placeholder="日志"/>
  </div>
</template>

<script>
import axios from 'axios'
// import {ElNotification} from 'element-plus'
import FileTranFunction from "@/utils/file_tran";
// import {Search} from '@element-plus/icons-vue'


export default {
  name: "windowPackageManage4Spec",
  props: {
    sessionToken: String,
    dir_name: String,
    dir_path: String,
    height: Number
  },
  components: {
    // Search
  },
  data() {
    return {
      form_script: {
        script_package_dir2rpm: "if [ ! -d /root/rpmbuild_temp ]; then mkdir /root/rpmbuild_temp; fi\n" +
            "tar zxf /root/rpmbuild.tar.gz -C /root/rpmbuild_temp\n" +
            "mkdir -p /root/rpmbuild/{SOURCES,SPECS}\n" +
            "cp /root/rpmbuild_temp/*/* /root/rpmbuild/SOURCES\n" +
            "cp /root/rpmbuild/SOURCES/*.spec /root/rpmbuild/SPECS\n" +
            "rpmbuild -bs /root/rpmbuild/SPECS/*\n" +
            "if [ ! -d /root/rpm_src ]; then mkdir /root/rpm_src; fi\n" +
            "mv /root/rpmbuild/SRPMS/* /root/rpm_src\n" +
            "rm -fr /root/rpmbuild.tar.gz\n" +
            "rm -fr /root/rpmbuild_temp\n" +
            "rm -fr /root/rpmbuild"
      },
      activeNameCVEInfo: '1',
      loading_issue_info: false,
      issue_info: [],
      issue_info_search: [],
      log_package: "",
      loading_package: false,
      package_result: {
        msg: "",
        package_name: "",
        package_link: ""
      }
    }
  },
  methods: {
    package_dir4rpm() {
      let obj_this = this
      obj_this.loading_package = true
      obj_this.package_result.package_name = ""
      obj_this.package_result.package_link = ""
      obj_this.log_package = "正在后台打包目录。。。"
      let data = {
        "session_token": obj_this.sessionToken,
        "dir_path": obj_this.dir_path
      }
      axios.post('/package_dir4rpm_axios', data, {responseType: "blob"}).then(function (response) {
        obj_this.log_package = "已取得目录压缩包，正在送往打包服务器，并进行打包。。。"
        let blob = new Blob([response.data], {type: "application/x-tar"});
        let server_link_package = obj_this.$store.state.obj_setting_map.rpm_package_server_link
        FileTranFunction.uploadBlob4Package(blob, server_link_package + "/rpmbuild_bs_by_tar_gz",
            {

              host: obj_this.$store.state.obj_setting_map.rpm_package_server_link_host,
              username: obj_this.$store.state.obj_setting_map.rpm_package_server_link_username,
              password: obj_this.$store.state.obj_setting_map.rpm_package_server_link_password,
              script_package_dir2rpm: obj_this.form_script.script_package_dir2rpm,
            }, function (package_response) {
              obj_this.package_result.msg = package_response.data.msg
              obj_this.task_moniter = setInterval(function () {
                obj_this.get_msg(package_response.data.data.task_id)
              }, 2000)
              // obj_this.package_result.package_link = server_link_package + '/' + package_response.data.data.file_link
              obj_this.log_package = "过程日志："
            })
      }).catch(function (error) { // 请求失败处理
        console.log(error);
      });
    },
    get_msg(task_id) {
      let data = {"task_id": task_id}
      let obj_this = this
      let server_link_package = obj_this.$store.state.obj_setting_map.rpm_package_server_link
      axios.post(server_link_package + 'get_msg4task', data).then(function (response) {
        if (response.data.data.length > 0)
          for (let i = 0; i < response.data.data.length; i++) {
            if (response.data.data[i].msg_type === "msg") {
              obj_this.log_package += '\n[' + response.data.data[i].timestamp + '] ' + response.data.data[i].msg
            } else if (response.data.data[i].msg_type === "link") {
              obj_this.package_result.package_link = server_link_package + '/' + response.data.data[i].link
              obj_this.package_result.package_name = response.data.data[i].filename
            }
            if (response.data.data[i].status_is_end) {
              console.log("所有数据接收完毕，关闭定时器")
              obj_this.loading_package = false
              window.clearInterval(obj_this.task_moniter)
            }
          }
      }).catch(function (error) { // 请求失败处理
        console.log(error);
      });
    }
  },
  mounted() {

  }
}
</script>

<style scoped>
#windowIssueShowContent {
  height: 500px;
  overflow: auto;
}
</style>